{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "50f680ff",
   "metadata": {},
   "source": [
    " # Converting a grid model from CIM CGMES (v2.4.15) to pandapower\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "25b136f3",
   "metadata": {},
   "source": [
    "Import the pandapower library and the neccessary methods for the conversion as follows:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5cb1fd35",
   "metadata": {
    "scrolled": true,
    "tags": []
   },
   "outputs": [],
   "source": [
    "import os\n",
    "\n",
    "from pandapower.run import runpp\n",
    "from pandapower.converter.cim import from_cim as cim2pp\n",
    "import warnings\n",
    "warnings.simplefilter(action='ignore', category=FutureWarning)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0f4cf251",
   "metadata": {},
   "source": [
    "## CIM CGMES to pandapower\n",
    "\n",
    "First we start by defining the path of the CGMES zip files, which can convert to PandaPower. When using the ENTSO-E test models, make sure to include the Boundary profiles as well. Then call the converter."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f066a777",
   "metadata": {
    "scrolled": true,
    "tags": []
   },
   "outputs": [],
   "source": [
    "# folder_path points to the directory where the CIM .zip-Files are stored:\n",
    "folder_path = os.path.join(os.getcwd(), 'example_cim')\n",
    "\n",
    "# cgmes_files is a list containing paths to both files needed for the CIM converter:\n",
    "cgmes_files = [os.path.join(folder_path, 'CGMES_v2.4.15_SmallGridTestConfiguration_Boundary_v3.0.0.zip'),\n",
    "               os.path.join(folder_path, 'CGMES_v2.4.15_SmallGridTestConfiguration_BaseCase_Complete_v3.0.0.zip')]\n",
    "\n",
    "for f in cgmes_files:\n",
    "    if not os.path.exists(f):\n",
    "        raise UserWarning(f\"Wrong path specified for the CGMES file {f}\")\n",
    "\n",
    "net = cim2pp.from_cim(file_list=cgmes_files, use_GL_or_DL_profile='DL')\n",
    "\n",
    "print('Conversion successful')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "80ec6e0d",
   "metadata": {},
   "source": [
    "## Run the power flow\n",
    "Once the network is converted to pandapower, we can run a power flow:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3eddf0bf",
   "metadata": {
    "scrolled": true,
    "tags": []
   },
   "outputs": [],
   "source": [
    "runpp(net)\n",
    "print(net.res_bus.iloc[0:5]) # print first few bus results"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "557a5273",
   "metadata": {},
   "source": [
    "## Plot the converted network and visualize results\n",
    "\n",
    "Preliminary requirements:\n",
    "* This plotting demo only works if the imported CIM network has geo data (GL/DL profile). If not, you can create generic coordinates using the following instructions:\n",
    "https://pandapower.readthedocs.io/en/latest/plotting/matplotlib/generic.html and then run the cells below.\n",
    "* Matplotlib is required (can be installed via conda)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "43e428fae8b77750",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "Some geocoordinates are missing, and the easiest way is to copy some over."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fc7b563bcf3b7db2",
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "bus_geo = net.bus.geo.iloc[0]\n",
    "net.bus.geo.fillna(bus_geo, inplace=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e53115d4ba68ddfc",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "Start plotting the grid"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "30c079c8",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "import pandapower.plotting as plt\n",
    "import pandapower.plotting.colormaps as cmaps\n",
    "\n",
    "cmap_list = [(0.9, \"blue\"), (1.0, \"green\"), (1.1, \"red\")]\n",
    "cmap, norm = cmaps.cmap_continuous(cmap_list)\n",
    "\n",
    "sizes = plt.get_collection_sizes(net)\n",
    "\n",
    "bc = plt.create_bus_collection(net,net.bus.index.values,cmap=cmap, size=sizes['bus'])\n",
    "lc = plt.create_line_collection(net,net.line.index.values, use_bus_geodata=True)\n",
    "tc = plt.create_trafo_collection(net,net.trafo.index.values, size=sizes[\"trafo\"])\n",
    "plt.draw_collections([bc,lc,tc])"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
